package com.wanmait.hotelManage.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.vo.Admin;

/**
 * Servlet Filter implementation class AdminFilter
 */
@WebFilter("/manage/index.jsp")
public class AdminFilter implements Filter {

    /**
     * Default constructor. 
     */
    public AdminFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		HttpServletResponse httpResponse = (HttpServletResponse)response;
	
		String path=httpRequest.getContextPath();
		Admin admin=(Admin)httpRequest.getSession().getAttribute("admin");
		if(admin==null){
			//将来时的路径保存到session中
			httpRequest.getSession().setAttribute("goUrl2", httpRequest.getHeader("Referer"));
			httpResponse.sendRedirect(path+"/manage/login.jsp");
			//跳转到login.jsp
//			PrintWriter out = httpResponse.getWriter();
//			out.print("login");
//			out.close();
		}
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
